package com.sajhasite.web;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sajhasite.domain.bean.ChildPostBean;
import com.sajhasite.domain.constant.CategoryType;
import com.sajhasite.domain.entity.ChildPost;
import com.sajhasite.domain.entity.Post;
import com.sajhasite.service.IPostService;

@Controller
@RequestMapping("/post")
public class PostController {
	
	

	@Inject
	private IPostService postService;

	public IPostService getPostService() {
    	return postService;
    }

	public void setPostService(IPostService postService) {
    	this.postService = postService;    	
    }
	
	@RequestMapping(value="/expandpost.do", method=RequestMethod.GET)
	public String  newForm(@RequestParam("postid") String postid, Model model) {
		Post post=this.postService.getAllChildPostForPostId(Long.parseLong(postid));
		model.addAttribute("post", post);	
		model.addAttribute("childpost",post.getChildPosts());
		model.addAttribute("childpostbean",new ChildPost());
		for(ChildPost cp:post.getChildPosts()){
		System.out.println(cp.toString());
		}
		return "expandpost";
	}
	

	
	
	@RequestMapping(value="/addnewpost.do", method=RequestMethod.GET)
	public String  addNewPost(Model model) {
		CategoryType[] categorylist=this.postService.getCategoryList();
		model.addAttribute("postbean", new Post());
		model.addAttribute("category", categorylist);
		model.addAttribute("title","");
		model.addAttribute("body", "");
		return "addnewpost";
	}
	
	@RequestMapping(value="/submitnewpost.do", method=RequestMethod.POST)
	public String  addNewPost(
			@ModelAttribute("postbean") Post post
						) {		
		System.out.println(post.getTitle());

		this.postService.addPost(post);
		return "home";
	}
	
	@RequestMapping(value="/submitnewchildpost.do", method=RequestMethod.POST)
	public String  addNewChildPost(
			@ModelAttribute("childpostbean")ChildPostBean childPostBean/*,@RequestParam("postid") String postid*/
						) {		
		
		
		this.postService.addChildPost(childPostBean);
		return "home";
	}
}
